package com.audaque.springboot.foshanupload.web.productdemo.controller;

import com.alibaba.excel.EasyExcel;
import com.audaque.springboot.foshanupload.authcore.anno.NeedLoginAnno;
import com.audaque.springboot.foshanupload.core.util.LocalDateTimeUtils;
import com.audaque.springboot.foshanupload.web.productdemo.bo.ProductImportBo;
import com.audaque.springboot.foshanupload.web.productdemo.doc.ProductEs;
import com.audaque.springboot.foshanupload.web.productdemo.entity.Rule;
import com.audaque.springboot.foshanupload.web.productdemo.listener.ProductImportBoListener;
import com.audaque.springboot.foshanupload.web.productdemo.service.IProductService;
import com.audaque.springboot.foshanupload.web.productdemo.service.IRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.messaging.MessagingException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;

@RestController

@Api(value = "商品 前端控制器")
@Slf4j
@RequestMapping("/productExt")
public class ProductExtendController {

    @Autowired
    private IProductService iProductService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private IRuleService iRuleService;


    @PostConstruct
    public void init() throws Exception {
        iProductService.createIndexAndPushMapping();
    }


    @ApiOperation("db2es")
    @PostMapping("db2es")
    public void db2es() throws Exception {
        iProductService.db2es();
    }


    @ApiOperation("excel2db")
    @PostMapping("excel2db")
    public void excel2mysql(@RequestParam("file") MultipartFile file) throws MessagingException, IOException {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(file.getInputStream(), ProductImportBo.class, new ProductImportBoListener(iProductService, threadPoolExecutor)).sheet().doRead();
    }


    @NeedLoginAnno
    @ApiOperation("测试")
    @GetMapping("test")
    public Page<ProductEs> test(@RequestParam("keyword") String keyword) throws Exception {
        Page<ProductEs> result = null;
        Rule rule = new Rule();
        long currentMilli = LocalDateTimeUtils.getCurrentMilli();
        rule.setFromTimestamp(currentMilli);
        rule.setToTimestamp(currentMilli + 1000 * 60 * 60);
        rule.setUserId("1");
        rule.setTimes(10L);
        iRuleService.save(rule);
        for (int i = 0; i < 1000; i++) {
            if (i == 0) {
                result = iProductService.multiMatch(keyword);
            }else{
                iProductService.multiMatch(keyword);
            }

        }
        return result;

    }


    @NeedLoginAnno
    @ApiOperation("多匹配查询文档")
    @GetMapping("multiMatch")
    public Page<ProductEs> multiMatch(@RequestParam("keyword") String keyword) throws Exception {
        return iProductService.multiMatch(keyword);
    }


}
